<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert">
  <div class="am-modal-dialog">
    <div class="am-modal-hd">提示</div>
    <div class="am-modal-bd">
      	请到合伙人授信规则管理增加产品参数
    </div>
    <div class="am-modal-footer">
      <span class="am-modal-btn">确定</span>
    </div>
  </div>
</div>
<div class="row-content am-cf">
	<div class="row">
		<div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
			<div class="widget am-cf">
				<div class="widget-head am-cf">
					<div class="widget-title am-fl" id="title">收益配置</div>
					<div class="widget-function am-fr">
						<a href="javascript:;" class="am-icon-cog"></a>
					</div>
				</div>
				<div class="widget-body am-fr">
					<form class="am-form tpl-form-line-form" id="agentForm" method="post">
						<div class="am-form-group" id="operationFeeId">
							<label for="title" class="am-u-sm-3 am-form-label">收益结算方式</label>
							<div class="am-u-sm-3">
							   <select id="settleWay" data-am-selected="{searchBox: 1}"  style="display: none;" name="settleWay" required="required" >
									<option th:each="settleWay : ${settleWayList}" 	     th:value="${settleWay.key}"    th:text="${settleWay.value}" th:selected="${settleWay.key==settleWay}" ></option>
								</select> 
							</div>
							<label for="title" class="am-u-sm-3 am-form-label">运营费用（%）</label>
							<div class="am-u-sm-3">
								<input type="text" id="operationFee" class="tpl-form-input" placeholder="请输入运营准备金" name="operationFee" required="required" /> 
							</div>
						</div>
						<input type="hidden" class="am-u-sm-3" id="agentId" name="agentId" th:value="${agentId}" />
						<input type="hidden" class="am-u-sm-3" id="type" name="type" th:value="${type}" />
					</form>
					<div class="am-form-group">
						<div class="am-u-sm-10 am-u-sm-push-6">
							<button type="button" onclick="javascript:$('#agentForm').submit();"
								class="am-btn am-btn-primary tpl-btn-bg-color-success ">提交</button>
									<button type="button" onclick="returnPage()"
								class="am-btn am-btn-success tpl-btn-bg-color-success ">返回</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>

	</div>
	<script th:inline="javascript">
	<![CDATA[
	var type = $('#type').val();
	var typeName;
	$(document).ready(function(){
		if(type=="income"){
			document.getElementById('title').innerHTML = "收益配置";
			typeName="收益";
		}else if(type=="recharge"){
			document.getElementById('title').innerHTML = "保证金配置";
			typeName="保证金";
			$('#operationFeeId').empty();
		}
		fromAppend();
		setProductValue();
		setOperationFee();
		$('#settleWay').selected();
	});
	
	function fromAppend(){
		var agentId = $('#agentId').val();
		$.ajax({
            type: "GET",
            url: "agentConfig/getProductList",
            dataType: "json",
            data:{"agentId":agentId},
            async: false,
            success: function(data){
            	var productList = data;
            	var agentForm=$('#agentForm'); //得到form对象
            	if(productList.length == 0){
            		$('#my-alert').modal({
            		      relatedTarget: this,
            		      onConfirm: function(e) {
            		        alert('你输入的是：' + e.data || '')
            		      },
            		      onCancel: function(e) {
            		        alert('不想说!');
            		      }
            		    });
            	}
        		for(var i = 0; i<productList.length ; i++){
        	        var tmpInput=$('<label for="label" class="am-u-sm-3 am-form-label">'+productList[i].productName+'</label><div class="am-form-group">'
        			+ '<label for="realName" class="am-u-sm-2 am-form-label">县级合伙人'+typeName+'（%）：</label>'
        			+ '<div class="am-u-sm-1"><input type="number" id="xian'+productList[i].type+'" name="xian'+productList[i].type+'" /></div>'
        			+ '<label for="mobile" class="am-u-sm-2 am-form-label">镇级合伙人'+typeName+'（%）：</label>'
        			+ '<div class="am-u-sm-1"><input type="number" id="zhen'+productList[i].type+'" name="zhen'+productList[i].type+'"  /> </div>'
        			+ '<label for="mobile" class="am-u-sm-2 am-form-label">村级合伙人'+typeName+'（%）：</label>'
        			+ '<div class="am-u-sm-1"><input type="number" id="cun'+productList[i].type+'" name="cun'+productList[i].type+'" /> </div></div>');
        	        agentForm.append(tmpInput);
        		}
             }
        });
	}
	
	function returnPage(){
		if(type=="income"){
			loadPage('agentConfig/agentIncomeUI');
		}else if(type=="recharge"){
			loadPage('agentConfig/agentRechargeUI');
		}
	}
	
	function setProductValue(){
		var agentId = $('#agentId').val();
		$.ajax({
            type: "GET",
            url: "agentConfig/getConfigValue",
            dataType: "json",
            async: false,
            data:{"agentId":agentId,"type":type},
            success: function(data){
            	for(var key in data) {
            		$('#'+key).val(data[key]);
            	} 
        	}
        });
	}
	
	function setOperationFee(){
		var agentId = $('#agentId').val();
		$.ajax({
            type: "GET",
            url: "agentConfig/getOperationFee",
            dataType: "json",
            async: false,
            data:{"agentId":agentId},
            success: function(data){
            	$('#operationFee').val(data.operationFee);
            	$('#settleWay').val(data.settleWay);
        	}
        });
	}
	
	function doSubmit(){
		var agentForm=$('#agentForm');
		agentForm.submit();
	}
	
	validateUserForm();
	function validateUserForm(){    
	    $('#agentForm').validate({
	        errorElement : 'span',
	        errorClass : 'am-icon-times',
	        focusInvalid : true,
	        onkeyup:false,
	        ignore : "",
	        rules : {
	        },
	        messages : {
	        },
	        submitHandler : function(form) {
	        	if(type=="income"){
	        		commit('agentForm', 'agentConfig/saveIncome','agentConfig/agentIncomeUI');
	    		}else if(type=="recharge"){
	    			commit('agentForm', 'agentConfig/saveIncome','agentConfig/agentRechargeUI');
	    		}
	        	
	        }
	    });
	}
		]]>
	</script>